﻿namespace Loader
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Collections;
    using porter;
    using Library;
    using Library.Definition;
    using Library.Interface;
    using System.Diagnostics;
    using Library.Heuristics.BowlingFeature;
    using Library.BallRelationHMM;
    using Library.RBCPreprocessor;

    public class Runner
    {
        public static void Main(string []args)
        {
            ////BowlingFeatureHeuristics bowlingHeursitic = new BowlingFeatureHeuristics();
            ////var dictionary = bowlingHeursitic.CountFrequency(@"C:\Users\Rishi\Documents\BTP\" + args[0] + ".txt");

            ////foreach (var keyValuePair in dictionary)
            ////{
            ////    Console.WriteLine("{0} : {1}", keyValuePair.Key, keyValuePair.Value);
            ////}

            ////BowlingFeature(@"C:\Users\Rishi\Documents\BTP\2007\text", BowlingFeatures.Line);
            ////BowlingFeature(@"C:\Users\Rishi\Documents\BTP\2008\text", BowlingFeatures.Line);
            ////BowlingFeature(@"C:\Users\Rishi\Documents\BTP\2009\text", BowlingFeatures.Line);
            ////BowlingFeature(@"C:\Users\Rishi\Documents\BTP\2010\text", BowlingFeatures.Line);
            ////BowlingFeature(@"C:\Users\Rishi\Documents\BTP\2011\text", BowlingFeatures.Line);
            ////BowlingFeature(@"C:\Users\Rishi\Documents\BTP\2012\text", BowlingFeatures.Line);        
            //////CreateStemmedFile(@"C:\Users\Rishi\Documents\BTP\2009\text");
            //CreateStemmedFile(@"C:\Users\Rishi\Documents\BTP\2010\text");
            //CreateStemmedFile(@"C:\Users\Rishi\Documents\BTP\2011\text");
            //CreateStemmedFile(@"C:\Users\Rishi\Documents\BTP\2012\text");

            //GetRelationalInstances();
          
        }

        private static void ProcessHMM()
        {

            HMMPreprocessor hmm = new HMMPreprocessor();
            hmm.PrintInstances(@"c:\users\rishi\documents\btp\all bowlers\Khan_Hayden_stemmed.txt");
            BallType ballType = new BallType();
            ballType.OutputBowlingBasedReport(@"C:\Users\Rishi\Documents\BTP\All Bowlers\RelatedDeliveries\Khan_Hayden_stemmed_related.txt", true);


            HMMPreprocessor.FormatRelationalInstancesWithBatsmanPlay(@"c:\users\rishi\documents\btp\all bowlers\Khan_Hayden_finaleee.txt");

        }

        private static void GetRelationalInstances()
        {
            for (int year = 2012; year <= 2012; year++)
            {
                var directoryName = @"C:\Users\Rishi\Documents\BTP\All Bowlers\";
                OutputRelationalInstances(directoryName);

                foreach (var file in Directory.EnumerateFiles(directoryName + "RelatedDeliveries"))
                {
                    var ballType = new BallType(BowlingFeatures.Length);
                    Tuple<int, int, int, IList<int>> lengthReport = ballType.OutputBowlingBasedReport(file, BowlingFeatures.Length, false, true);
                    ballType = new BallType(BowlingFeatures.Line);
                    lengthReport = ballType.OutputBowlingBasedReport(file, BowlingFeatures.Line, false, true);

                    File.Delete(file);
                    string outFileNameLength = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + "_" + BowlingFeatures.Length + "_transition.txt");
                    BallRelationalDependenceCheck.FormatRelationalInstances(outFileNameLength);
                    string outFileNameLine = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + "_" + BowlingFeatures.Line + "_transition.txt");
                    BallRelationalDependenceCheck.FormatRelationalInstances(outFileNameLine);
                    File.Delete(outFileNameLength);
                    File.Delete(outFileNameLine);
                    var ballRelationCheck = new BallRelationalDependenceCheck();
                    var inFileLength = Path.Combine(Path.GetDirectoryName(outFileNameLength), Path.GetFileNameWithoutExtension(outFileNameLength) + "_final.txt");
                    var inFileLine = Path.Combine(Path.GetDirectoryName(outFileNameLine), Path.GetFileNameWithoutExtension(outFileNameLine) + "_final.txt");
                    ballRelationCheck.ReportTransitionMatrix(BowlingFeatures.Length, inFileLength);
                    ballRelationCheck.ReportTransitionMatrix(BowlingFeatures.Line, inFileLine);
                    File.Delete(inFileLength);
                    File.Delete(inFileLine);

                }
            }
        }
        public static void BowlingFeature(string inputDirectory, BowlingFeatures bowlingFeature)
        {                            
            inputDirectory = inputDirectory  + @"\stemmed";
            var outputDirectory = Path.Combine(inputDirectory, bowlingFeature.ToString());
            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }

            var ballType = new BallType(bowlingFeature);

            int BattingAidedFeatures = 0;
            int NormalFeatures = 0;
            int NoInfo = 0;
            IList<int> featureSpecificationList = new int[10];
            
            foreach (var inputFile in Directory.EnumerateFiles(inputDirectory))
            {                
                Tuple<int, int, int, IList<int> > lengthReport = ballType.OutputBowlingBasedReport(inputFile, bowlingFeature, false, false);
                BattingAidedFeatures += lengthReport.Item1;
                NormalFeatures += lengthReport.Item2;
                NoInfo += lengthReport.Item3;
                AddtoFeatureSpecifications(featureSpecificationList, lengthReport.Item4);
            }
            Console.WriteLine("Batting Aided {1} Features = {0}", BattingAidedFeatures, bowlingFeature.ToString());
            Console.WriteLine("Normal {1} Features = {0}", NormalFeatures, bowlingFeature.ToString());
            Console.WriteLine("No {1} Information = {0}", NoInfo, bowlingFeature.ToString());
            Console.WriteLine("Total Deliveries = {0}", BattingAidedFeatures + NormalFeatures + NoInfo);
            Console.WriteLine("Improvement = {0}%", (BattingAidedFeatures * 100 / Convert.ToDouble(BattingAidedFeatures + NormalFeatures + NoInfo)));
            Console.WriteLine();

            for (int i = 0; i < featureSpecificationList.Count; i++)
            {
                if (featureSpecificationList[i] > 0)
                {
                    Console.WriteLine("Type {0} : Number of deliveries = {1}", i, featureSpecificationList[i]);
                }
            }
        }

        private static void AddtoFeatureSpecifications(IList<int> totalFeatureSpecificationList, IList<int> featureSpecificationListInstance)
        {
            for (int i = 0; i < totalFeatureSpecificationList.Count; i++)
            {
                totalFeatureSpecificationList[i] += featureSpecificationListInstance[i];
            }
        }

        public static void OutputRelationalInstances(string directoryName)
        {
            var inputDirectory = directoryName;
            ////CreateStemmedFile(inputDirectory);
            ////inputDirectory = Path.Combine(inputDirectory, "stemmed");
            var relationalDependency = new BallRelationalDependenceCheck();
            foreach(var inputFile in Directory.EnumerateFiles(inputDirectory))
            {
                relationalDependency.PrintInstances(inputFile);
            }
        }

        public static void CreateStemmedFile(string inputDirectory)
        {
            if (!Directory.Exists(Path.Combine(inputDirectory, "stemmed")))
            {
                Directory.CreateDirectory(Path.Combine(inputDirectory, "stemmed"));
            }
            foreach (var inputFile in Directory.EnumerateFiles(inputDirectory))
            {
                var outputFile = Path.Combine(inputDirectory, "stemmed", Path.GetFileNameWithoutExtension(inputFile) + "_stemmed.txt");
                Stemmer.StemFile(inputFile, outputFile);
            }
            
        }

        static void LaunchCommandLineApp(string command)
        {          
            ProcessStartInfo startInfo = new ProcessStartInfo();            
            startInfo.UseShellExecute = false;
            startInfo.FileName = command;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;            
            try
            {
                using (Process exeProcess = Process.Start(startInfo))
                {
                    exeProcess.WaitForExit();
                }
            }
            catch
            {             
            }
        }
    }
}
